package com.tidc.seaserverservice.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tidc.seaservermapper.dao.CommitmentDao;
import com.tidc.seaservermapper.dao.RabbitMessageLogDao;
import com.tidc.seaservermapper.dao.UserDao;
import com.tidc.seaservermapper.dao.UserPackageDao;
import com.tidc.seaservermodel.config.RabbitConfig;
import com.tidc.seaservermodel.entity.*;
import com.tidc.seaservermodel.exception.TidcBadParent;
import com.tidc.seaservermodel.exception.TidcRestException;
import com.tidc.seaservermodel.properties.QuartzProperties;
import com.tidc.seaservermodel.util.RestUtil;
import com.tidc.seaservermodel.util.SpringUtil;
import com.tidc.seaserverservice.service.SecondaryPackageService;
import com.tidc.seaserverservice.util.RabbitUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ：冯涛滔
 * @date ：Created in 2020/9/28 19:58
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class SecondaryPackageServiceImpl implements SecondaryPackageService {
    @Resource
    private UserPackageDao userPackageDao;
    @Resource
    private UserDao userDao;
    @Resource
    private CommitmentDao commitmentDao;
    @Resource
    private RestUtil restUtil;
    @Resource
    private QuartzProperties quartzProperties;
    @Resource
    private ObjectMapper objectMapper;
    @Resource
    private RabbitUtil rabbitUtil;
    @Resource
    private RabbitMessageLogDao rabbitMessageLogDao;
    @Override
    public Fres<Map<Object, Object>> getUserPackage(int pageSize,int pageNum,Authentication authentication) throws TidcBadParent {
        int count = userPackageDao.getUserPackageCount(Integer.parseInt(authentication.getName()));
        Map<Object, Object> map = SpringUtil.pagingPrepare(pageSize, pageNum, count);
        List<UserPackage> userPackageByLimit = userPackageDao.getUserPackageByLimit((pageNum - 1) * pageSize, pageSize, Integer.parseInt(authentication.getName()));
        map.put("list",userPackageByLimit);
        return Fres.Success(map,"成功");
    }
//          TODO 增加一个使用记录
    @Transactional
    @Override
    public Fres<String> useShop(UserPackage userPackage, Authentication authentication) throws TidcRestException, JsonProcessingException {
        Fres<String> fres = null;
        User user = userDao.queryById(Integer.valueOf(authentication.getName()));
        UserPackage check = userPackageDao.queryById(userPackage.getId(),user.getId());
        if(userPackage.getAmount()>0 && check.getAmount()>=userPackage.getAmount()){
            userPackageDao.useShop(userPackage.getAmount(),userPackage.getId());
//          给一级用户增加待兑换列表
            Commitment commitment = Commitment.addCommitment(check, userPackage.getAmount(),user.getSuperiorUserId());
            commitmentDao.insert(commitment);
//            请求定时组件
            restUtil.post(commitment, quartzProperties.getUrl(),quartzProperties.getCheckCommitmentOutUri());
            Email email = Email.secondaryUseShop(user.getUsername(),userPackage.getAmount(),check.getShopName(),user.getSuperiorUserId());
            RabbitMessageLog rabbitMessageLog = RabbitMessageLog.sendEmail(objectMapper.writeValueAsString(email));
            rabbitUtil.sendMQ(rabbitMessageLog);
            fres  = Fres.Success();
        }else{
            fres = Fres.badRequestParent("","数量不能为0或背包里的奖励数量不足");
        }
        return fres;
    }
}
